/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EL_PROTOCOL_DEVICE_COMMAND_HANDLER_H
#define EL_PROTOCOL_DEVICE_COMMAND_HANDLER_H

#include <string>
#include <functional>
#include <unordered_map>
#include <memory>
#include <nlohmann/json.hpp>
#include "web_server.h"
#include "network.h"
#include "command_handler_base.h"  // 添加基类头文件

namespace El {
namespace Protocol {

/**
 * @brief 设备协议处理器
 * 处理设备管理、视频流获取、事件订阅等相关协议
 */
class DeviceCommandHandler : public CommandHandlerBase {  // 继承自基类
public:
    DeviceCommandHandler() = default;
    ~DeviceCommandHandler() = default;

    // 禁止拷贝和赋值
    DeviceCommandHandler(const DeviceCommandHandler &) = delete;
    DeviceCommandHandler &operator=(const DeviceCommandHandler &) = delete;

    /**
     * @brief 注册方法处理器到方法映射表
     * @param method_handlers 目标方法映射表
     */
    void RegisterMethodHandlers(
        std::unordered_map<std::string, std::function<nlohmann::json(const std::string &, const nlohmann::json &)>>
            &method_handlers);

private:
    // 设备管理相关方法处理器
    nlohmann::json HandleSystemGetInfo(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleCamerasGetStream(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleEventsSubscribe(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleEventsUnsubscribe(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleSystemGetTime(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleSystemSetTime(const std::string &id, const nlohmann::json &params);
    
    // NTP 相关方法处理器
    nlohmann::json HandleSystemGetNTPConfig(const std::string &id, const nlohmann::json &params);
    nlohmann::json HandleSystemSetNTPConfig(const std::string &id, const nlohmann::json &params);
    
    // 出厂重置相关方法处理器
    nlohmann::json HandleSystemFactoryReset(const std::string &id, const nlohmann::json &params);
    
    // 系统重启相关方法处理器
    nlohmann::json HandleSystemRestart(const std::string &id, const nlohmann::json &params);

    // 配置管理由 ConfigCommandHandler 统一处理

    // 存储事件订阅信息的结构
    struct Subscription {
        std::string id;
        std::string event_type;
        std::string callback_url;
        nlohmann::json filters;
        bool active;
        std::string created_at;
    };

    // 事件订阅管理
    std::unordered_map<std::string, Subscription> subscriptions_;
};

} // namespace Protocol
} // namespace El

#endif // EL_PROTOCOL_DEVICE_COMMAND_HANDLER_H
